﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Properties;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Render;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationExportAsWmf : IOperation
    {
    //    private Dictionary<ModelCell, ImageExportInfo> settings;

        public OperationExportAsWmf()
        {
            //settings = new Dictionary<ModelCell, ImageExportInfo>();
        }
        #region IOperation Members

        public string Name
        {
            get { return "Экспорт в Wmf"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.Other; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.WmfExport.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            List<ModelCell> modelsToExport = new List<ModelCell>();
            foreach (IComponent component in modelControl.ModelContainer.InnerComponents)
                modelsToExport.Add(component as ModelCell);

            ImageExportForm form = new ImageExportForm(modelsToExport, null);

            if (form.ShowDialog() != DialogResult.OK)
                return;

            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Выберите папку для сохранения изображений";
            folderDialog.ShowNewFolderButton = true;

            if (folderDialog.ShowDialog() != DialogResult.OK)
                return;

            ModelCoordinateConverter coordinateConverter = new ModelCoordinateConverter();
            DraftRender render = new DraftRender();

            foreach (ImageExportInfo info in form.ImageExportInfo)
            {
                if (!info.Enabled)
                    continue;

                //info.Model.Update(info.DimensionsPreferences);
                render.SetDimensionsPreferences(info.DimensionsPreferences);

                RectangleF modelRect = info.Model.Model.BoundingBox;
                ModelPoint center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                RectangleF screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                Bitmap bitmap = new Bitmap((int)screenRect.Width, (int)screenRect.Height);
                Graphics ig = Graphics.FromImage(bitmap);

                render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(info.DimensionsPreferences.Font);

                modelRect = render.GetModelBoundingBoxAfterDrawing(info.Model, (int)screenRect.Width, (int)screenRect.Height);
                center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(info.DimensionsPreferences.Font);
                render.Visit(info.Model);
                IntPtr hdc = ig.GetHdc();
                Metafile metafile = new Metafile(folderDialog.SelectedPath + "\\" + info.Model.Name + ".wmf", hdc, screenRect, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
                ig.ReleaseHdc();
                Graphics mg = Graphics.FromImage(metafile);
                mg.PageUnit = GraphicsUnit.Pixel;
                mg.Clear(Color.White);
                render.Initialize(mg, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                render.SetSizesFont(info.DimensionsPreferences.Font);
                render.Visit(modelControl.ModelContainer);
       
                mg.Dispose();
                ig.Dispose();

               // info.Model.Update(GlobalPreferences.Preferences.EditorDimensions);
            }
        }

        public bool CanUndo
        {
            get { return false; }
        }

        #endregion
    }
}
